//
//  TBRSelfDrivePickerC.m
//  TBRentClient
//
//  Created by tonbright on 2018/5/15.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRSelfDrivePickerC.h"
#import "YXDateHelpObject.h"
@interface TBRSelfDrivePickerC ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property (nonatomic,strong)NSMutableArray *dateArray;
//@property (nonatomic,strong)NSMutableArray *endDateArray;
@property (nonatomic,strong)NSArray *hourArray;
@property (nonatomic,strong)NSArray *minArray;
@property (weak, nonatomic) IBOutlet UILabel *lbe_desc;

@end

@implementation TBRSelfDrivePickerC

- (void)viewDidLoad {
    [super viewDidLoad];
    NSInteger startIndex = [AppSetting shareAppSetting].selStartIndex;
    NSInteger timeDuration = [AppSetting shareAppSetting].timeDuration;
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    NSInteger index = 26;//选择的所有时间---------
    if (_isStartTime) {
        index = 26;
        _lbe_desc.text = @"请选择取车时间";
    }else {
        index = 30;
    }
    self.dateArray = [[YXDateHelpObject manager] latelyTime:index].mutableCopy;
//    self.endDateArray = [[YXDateHelpObject manager] latelyTime:60].mutableCopy;
    //去除第一天
    if (_isStartTime) {
       [self.dateArray removeObjectAtIndex:0];
    }else {
        for (NSInteger i = 0; i < startIndex + 2; i++) {
            [self.dateArray removeObjectAtIndex:0];
        }
    }
    _minArray = @[@"00",@"10",@"20",@"30",@"40",@"50"];
    NSMutableArray *hourArray = [NSMutableArray array];
    for (int i = 0; i < 24; i++) {
        NSString *iStr = [NSString stringWithFormat:@"%d",i];
        [hourArray addObject:iStr];
    }
    _hourArray = hourArray;
    if (_isStartTime) {
       [self.pickerView selectRow:startIndex inComponent:0 animated:NO];
    }else {
       [self.pickerView selectRow:timeDuration - 1 inComponent:0 animated:NO];
    }
    [self.pickerView selectRow:8 inComponent:1 animated:NO];
    [self.pickerView selectRow:0 inComponent:2 animated:NO];
//    [self pickerView:_pickerView didSelectRow:0 inComponent:0];  为啥会报错
//    [self pickerView:_pickerView didSelectRow:0 inComponent:1];
//    [self pickerView:_pickerView didSelectRow:0 inComponent:2];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.dateArray.count;
//        if (_isStartTime) {
//           return self.dateArray.count;
//        }
//        return self.endDateArray.count;
    }else if (component == 1) {
        return self.hourArray.count;
    }
    return self.minArray.count;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return Width / 2;
    }else {
        return Width / 4;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return self.dateArray[row];
//        if (_isStartTime) {
//            return self.dateArray[row];
//        }
//        return self.endDateArray[row];
    }else if (component == 1) {
        NSString *tmpHour = [NSString stringWithFormat:@"%@时",self.hourArray[row]];
        return tmpHour;
    }
    NSString *tmpMin = [NSString stringWithFormat:@"%@分",self.minArray[row]];
    return tmpMin;
}

- (IBAction)cancleBtnClick:(UIButton *)sender {
    if (sender.tag == 10) {
        if (self.pickerViewCancleBtnBlock) {
            self.pickerViewCancleBtnBlock();
        }
    }else if (sender.tag == 20) {
        NSInteger dateIndex = [self.pickerView selectedRowInComponent:0];
        NSInteger hourIndex = [self.pickerView selectedRowInComponent:1];
        NSInteger minIndex = [self.pickerView selectedRowInComponent:2];
        if (self.pickerViewSureBtnBlock) {
            
            NSString *dateStr = self.dateArray[dateIndex];
            NSString *hourStr = self.hourArray[hourIndex];
            NSString *minStr = self.minArray[minIndex];
            NSString *yearStr = [[YXDateHelpObject manager] getStrFromDateFormat:@"yyyy" Date:[NSDate date]];
            NSString *currentMStr = [[YXDateHelpObject manager] getCurentTime:@"M" dateN:[NSDate date]];
            NSArray *tmpWeekArray = [dateStr componentsSeparatedByString:@" "];
            NSString *weekDayStr = tmpWeekArray.lastObject;
            NSString *selMStr = [[YXDateHelpObject manager] getStrDateFromStrDate:tmpWeekArray.firstObject OldFormat:@"M月d日" ByNewFormat:@"M"];
            NSString *selMDStr = [[YXDateHelpObject manager] getStrDateFromStrDate:tmpWeekArray.firstObject OldFormat:@"M月d日" ByNewFormat:@"M/d"];
            //这个时候说明选择的明年------------------
            if (currentMStr.integerValue == 12 && selMStr.integerValue < 11) {
                yearStr = [NSString stringWithFormat:@"%ld",[yearStr integerValue]+1];
            }
            
            
            if (_isStartTime) {
                [TBRRentAllMessageModel shareAllMessageModel].startTime = [NSString stringWithFormat:@"%@/%@ %@:%@",yearStr,selMDStr,hourStr,minStr];
                self.pickerViewSureBtnBlock(dateStr,hourStr,minStr,weekDayStr);
                [AppSetting shareAppSetting].selStartIndex = dateIndex;
                
            }else {
                [TBRRentAllMessageModel shareAllMessageModel].endTime = [NSString stringWithFormat:@"%@/%@ %@:%@",yearStr,selMDStr,hourStr,minStr];
                self.pickerViewSureBtnBlock(dateStr,hourStr,minStr,weekDayStr);
            }
            //根据开始和结束时间计算时间间隔
            
            
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.pickerViewCancleBtnBlock) {
        self.pickerViewCancleBtnBlock();
    }
}

@end
